package utils;


import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import application.AppApplication;
import application.HttpUrlConstant;
import bean.DoListNotifySourceBean;
import bean.ImBean;
import bean.NotifyNumBean;
import http.BaseCallback;
import http.OkHttpHelper;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ThinkPad on 2017/11/13.
 * 小红点消息数据处理的utils---即数据小于0返回0
 */

public class TipsUtils {
    private static OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();

    public static String fixNum(int num) {
        if (num < 0) {
            return "0";
        } else if (num > 99) {
            return "99+";
        }
        return String.valueOf(num);
    }

    /**
     * 获取未读数量
     */
    public static void getTabTips(final onGetTipNumSuccess onGetTipNumSuccess) {

        String url = HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.do_ListNotifySource);
        Map<String, String> params = new HashMap<>();
        params.put("__session__", AppApplication.getSession());
        okHttpHelper.post(url, params, new BaseCallback<DoListNotifySourceBean>() {
            @Override
            public void onRequestBefore(Request request) {

            }

            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onSuccess(Response response, DoListNotifySourceBean doListNotifySourceBean) {
                if (doListNotifySourceBean.isSucceed()) {
                    List<DoListNotifySourceBean.NotifySourcesBean> notifyList = doListNotifySourceBean.getNotifySources();
                    Gson gson = new Gson();
                    String str = gson.toJson(notifyList);
                    SharePreferHelper.setStringValues("tipList", str);

                    if (notifyList != null && notifyList.size() > 0) {
                        for (DoListNotifySourceBean.NotifySourcesBean sourcesBean : notifyList) {
                            if(sourcesBean.getAppCode().equals("Mobile.Workflow")){
                                continue;
                            }
                            getNotifyNum(sourcesBean.getAppCode(), sourcesBean.getNSUrl(), onGetTipNumSuccess);
                        }
                    }
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    /**
     * 获取未读的数目
     *
     * @param appCode
     * @param nsUrl              ---未读的网址
     * @param onGetTipNumSuccess
     */
    public static void getNotifyNum(final String appCode, String nsUrl, final onGetTipNumSuccess onGetTipNumSuccess) {
        String url = HttpUrlConstant.getWebSiteFullUrl("Apps/" + appCode + "/" + nsUrl);
        Map<String, String> params = new HashMap<>();
        params.put("__session__", AppApplication.getSession());
        okHttpHelper.post(url, params, new BaseCallback<NotifyNumBean>() {
            @Override
            public void onRequestBefore(Request request) {

            }

            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onSuccess(Response response, NotifyNumBean notifyNumBean) {
                String num = "0";
                if (notifyNumBean.getData() != null) {
                    num = fixNum(notifyNumBean.getData().getCount());
                }

                if (onGetTipNumSuccess != null) {
                    onGetTipNumSuccess.setOnGetTipNumSuccess(appCode, num);
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    public interface onGetTipNumSuccess {
        public void setOnGetTipNumSuccess(String AppCode, String num);
    }

    /**
     * 获取未读的IM数量
     *
     * @param onGetTipNumSuccess
     */
    public static void getIMUnReadTotalNum(final onGetTipNumSuccess onGetTipNumSuccess) {

        String url = HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.GetConversationList);
        Map<String, String> params = new HashMap<>();
        params.put("__session__", AppApplication.getSession());
        okHttpHelper.post(url, params, new BaseCallback<ImBean>() {


            @Override
            public void onRequestBefore(Request request) {

            }

            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onSuccess(Response response, ImBean imBean) {
                if (imBean.isSucceed() && imBean.getData() != null) {
                    int num = 0;
                    for (ImBean.DataBean dataBean : imBean.getData()) {
                        num += dataBean.getUnreadCount();
                    }
                    String imNum = fixNum(num);
                    if (onGetTipNumSuccess != null) {
                        onGetTipNumSuccess.setOnGetTipNumSuccess("IM", imNum);
                    }
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

}
